﻿using ChineseSchool.BLL;
using ChineseSchool.Models.Hethong;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ChineseSchool.Web
{
    public partial class Lessson : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["page"] != null)
                {
                    if (Util.CheckInteger(Request.QueryString["page"]))
                    {
                        LoadLessonsList(Convert.ToInt32(Request.QueryString["page"]));
                    }
                    else
                        LoadLessonsList(1);
                }
                else
                    LoadLessonsList(1);
            }
        }

        public void LoadLessonsList(int pageIndex)
        {
            var lstLessons = new B_Lessons().GetAllWithPaging(pageIndex, 10);

            // Bind data
            rptLessons.DataSource = lstLessons.Items;
            rptLessons.DataBind();

            // Bind Pager
            GeneratePager(pageIndex, Convert.ToInt32(lstLessons.TotalPages));
        }

        public void GeneratePager(int pageIndex, int totalPage)
        {
            List<ListItem> pageLinkContainer = new List<ListItem>();

            //pageLinkContainer.Add(new ListItem("First", "1"));
            for (int i = 1; i <= totalPage; i++)
            {
                pageLinkContainer.Add(new ListItem(i.ToString(), i.ToString(), pageIndex != i));
            }
            //pageLinkContainer.Add(new ListItem("Last", totalPage.ToString()));

            rptPager.DataSource = pageLinkContainer;
            rptPager.DataBind();
        }


        protected void RptLessonsItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var lnkDetail = (HyperLink)e.Item.FindControl("lnkLessonDetails");

            var id = ((M_Lessons)e.Item.DataItem).LessonID;
            var title = ((M_Lessons)e.Item.DataItem).LessonTitle;

            lnkDetail.NavigateUrl = Util.MakeLinkLessonDetail(id.ToString(CultureInfo.InvariantCulture), Encode.Uni2KD(title.ToString(CultureInfo.InvariantCulture)));
        }

        protected void RptPagerItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var lnkDetail = (HyperLink)e.Item.FindControl("lnkPageNo");

            lnkDetail.NavigateUrl = "/tieng-trung/minh-a/26/Bai-hoc-CLB.html?page=" + lnkDetail.Text;
        }
    }
}